/* Copyright (c) 2009 Matthias Kaeppler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * 
 * 
 * Modified for use in Mealssenger by Theodore Felix Leo
 */

package com.tfleo.mealssenger.adapter;

import java.util.List;

import com.github.ignition.core.widgets.RemoteImageView;
import com.tfleo.mealssenger.FoodActivity;
import com.tfleo.mealssenger.PlaceActivity;
import com.tfleo.mealssenger.R;
import com.tfleo.mealssenger.object.Food;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;


/**
 * Can be used as an adapter for an Android {@link Gallery} view. This adapter loads the images to
 * be shown from the web.
 * 
 * @author Matthias Kaeppler
 */
public class MenuGalleryAdapter extends WebGalleryAdapter {
    private LayoutInflater mInflater;

    /**
     * @param context
     *            the current context
     * @param imageUrls
     *            the set of image URLs which are to be loaded and displayed
     */
    public MenuGalleryAdapter(Context context, List<Food> objects, LayoutInflater inflater) {
    	super(context, objects, inflater);
        mInflater = inflater;
    }

    // TODO: both convertView and ViewHolder are pointless at the moment, since there's a framework
    // bug which causes views to not be cached in a Gallery widget:
    // http://code.google.com/p/android/issues/detail?id=3376
    
    /* Modified by Theodore Felix Leo for use within Mealssenger */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Food food = (Food) getItem(position);

        ViewHolder holder = null;
        RemoteImageView image = null;
        TextView name = null;
        TextView description = null;
        TextView likes = null;

        if (convertView == null) {
        	convertView = mInflater.inflate(R.layout.menu_chat_entry, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
        }
        
        holder = (ViewHolder) convertView.getTag();
        image = holder.getImageView();
        name = holder.getNameView();
        description = holder.getDescriptionView();
        likes = holder.getLikesView();
        
        // calling reset is important to prevent old images from displaying in a recycled view.
        image.reset();
        image.setImageUrl(food.imageUrl);
        image.loadImage();
        
        name.setText(food.name);
        description.setText(food.description);
        likes.setText(food.numEaten + " people ate this");
        
        onGetView(position, convertView, parent);

        return convertView;
    }

    protected void onGetView(int position, View convertView, ViewGroup parent) {
        // for extension
    }

    private class ViewHolder {
		private View vRow;
		private TextView vName = null;
		private TextView vDescription = null;
		private TextView vLikes = null;
		public RemoteImageView vImage = null;

		public ViewHolder(View row) {
			vRow = row;
		}

		public TextView getNameView() {
			if (vName == null) {
				vName = (TextView) vRow.findViewById(R.id.name);
			}
			return vName;
		}
		
		public TextView getDescriptionView() {
			if (vDescription == null) {
				vDescription = (TextView) vRow.findViewById(R.id.desc);
			}
			return vDescription;
		}
		
		public TextView getLikesView() {
			if (vLikes == null) {
				vLikes = (TextView) vRow.findViewById(R.id.like);
			}
			return vLikes;
		}

		public RemoteImageView getImageView() {
			if (vImage == null) {
				vImage = (RemoteImageView) vRow.findViewById(R.id.img);
			}
			return vImage;
		}
	}
}
